package com.zj.osm.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.zj.osm.App;
import com.zj.osm.R;

import java.lang.reflect.Field;

import static com.zj.osm.App.FONT_SIZE;

/**
 * Activity的基类
 *
 * @author jiang zhu on 2019/10/22
 */
public abstract class BaseActivity extends AppCompatActivity {

    private static final String INTENT_BUNDLE_TAG = "intent_bundle";
    protected final String TAG = getClass().getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //解决7.0以上版本状态栏变灰问题
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            try {
                @SuppressLint("PrivateApi") Class decorViewClazz = Class.forName("com.android.internal.policy.DecorView");
                Field field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor");
                field.setAccessible(true);
                field.setInt(getWindow().getDecorView(), Color.TRANSPARENT);  //改为透明
            } catch (Exception ignored) {}
        }
        App.getInstances().addActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        if (intent != null) {
            getAnim();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        getAnim();
    }

    /**
     * 无参跳转
     *
     * @param clazz 需要跳转的类
     */
    protected void startIntent(Class<?> clazz) {
        if (clazz == null) {
            return;
        }
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    /**
     * 有参跳转
     *
     * @param clazz  需要跳转的类
     * @param bundle 需要传的参数
     */
    protected void startIntent(Class<?> clazz, Bundle bundle) {
        if (clazz == null) {
            return;
        }
        Intent intent = new Intent(this, clazz);
        intent.putExtra(INTENT_BUNDLE_TAG, bundle);
        startActivity(intent);
    }

    /**
     * 获取Bundle值
     *
     * @param intent 传入intent
     * @return bundle
     */
    protected Bundle getBundle(Intent intent) {
        return intent.getBundleExtra(INTENT_BUNDLE_TAG);
    }

    /**
     * 设置跳转和返回活动动画
     */
    private void getAnim() {
        if (this.getParent() != null) {
            this.getParent().overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_from_right);
        } else {
            this.overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_from_right);
        }
    }


    /**
     * 禁止app字体大小跟随系统字体大小调节
     *
     * @return /
     */
    @Override
    public Resources getResources() {
        //禁止app字体大小跟随系统字体大小调节
        Resources resources = super.getResources();
        if (resources != null && resources.getConfiguration().fontScale != 1.0f) {
            Configuration configuration = resources.getConfiguration();
            configuration.fontScale = FONT_SIZE;
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }
        return resources;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (null != this.getCurrentFocus()) {
            //点击空白位置 隐藏软键盘
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            assert mInputMethodManager != null;
            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }


    @Override
    public void finish() {
        super.finish();
        getAnim();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        App.getInstances().removeActivity(this);
        finish();
    }


}
